1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.primitives;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkElementIndex;
21 import static com.google.common.base.Preconditions.checkNotNull;
22 import static com.google.common.base.Preconditions.checkPositionIndexes;
23 import static java.lang.Double.NEGATIVE_INFINITY;
24 import static java.lang.Double.POSITIVE_INFINITY;
25
26 import com.google.common.annotations.Beta;
27 import com.google.common.annotations.GwtCompatible;
28 import com.google.common.base.Converter;
29
30 import java.io.Serializable;
31 import java.util.AbstractList;
32 import java.util.Collection;
33 import java.util.Collections;
34 import java.util.Comparator;
35 import java.util.List;
36 import java.util.RandomAccess;
37
38
39
40
41
42
43
44
45
46
47
48
49 @GwtCompatible(emulated = true)
50 public final class Doubles {
51 private Doubles() {}
52
53
54
55
56
57
58
59 public static final int BYTES = Double.SIZE / Byte.SIZE;
60
61
62
63
64
65
66
67
68 public static int hashCode(double value) {
69 return ((Double) value).hashCode();
70
71
72
73 }
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 public static int compare(double a, double b) {
91 return Double.compare(a, b);
92 }
93
94
95
96
97
98
99
100
101 public static boolean isFinite(double value) {
102 return NEGATIVE_INFINITY < value & value < POSITIVE_INFINITY;
103 }
104
105
106
107
108
109
110
111
112
113
114
115 public static boolean contains(double[] array, double target) {
116 for (double value : array) {
117 if (value == target) {
118 return true;
119 }
120 }
121 return false;
122 }
123
124
125
126
127
128
129
130
131
132
133
134 public static int indexOf(double[] array, double target) {
135 return indexOf(array, target, 0, array.length);
136 }
137
138
139 private static int indexOf(
140 double[] array, double target, int start, int end) {
141 for (int i = start; i < end; i++) {
142 if (array[i] == target) {
143 return i;
144 }
145 }
146 return -1;
147 }
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163 public static int indexOf(double[] array, double[] target) {
164 checkNotNull(array, "array");
165 checkNotNull(target, "target");
166 if (target.length == 0) {
167 return 0;
168 }
169
170 outer:
171 for (int i = 0; i < array.length - target.length + 1; i++) {
172 for (int j = 0; j < target.length; j++) {
173 if (array[i + j] != target[j]) {
174 continue outer;
175 }
176 }
177 return i;
178 }
179 return -1;
180 }
181
182
183
184
185
186
187
188
189
190
191
192 public static int lastIndexOf(double[] array, double target) {
193 return lastIndexOf(array, target, 0, array.length);
194 }
195
196
197 private static int lastIndexOf(
198 double[] array, double target, int start, int end) {
199 for (int i = end - 1; i >= start; i--) {
200 if (array[i] == target) {
201 return i;
202 }
203 }
204 return -1;
205 }
206
207
208
209
210
211
212
213
214
215
216 public static double min(double... array) {
217 checkArgument(array.length > 0);
218 double min = array[0];
219 for (int i = 1; i < array.length; i++) {
220 min = Math.min(min, array[i]);
221 }
222 return min;
223 }
224
225
226
227
228
229
230
231
232
233
234 public static double max(double... array) {
235 checkArgument(array.length > 0);
236 double max = array[0];
237 for (int i = 1; i < array.length; i++) {
238 max = Math.max(max, array[i]);
239 }
240 return max;
241 }
242
243
244
245
246
247
248
249
250
251
252 public static double[] concat(double[]... arrays) {
253 int length = 0;
254 for (double[] array : arrays) {
255 length += array.length;
256 }
257 double[] result = new double[length];
258 int pos = 0;
259 for (double[] array : arrays) {
260 System.arraycopy(array, 0, result, pos, array.length);
261 pos += array.length;
262 }
263 return result;
264 }
265
266 private static final class DoubleConverter
267 extends Converter<String, Double> implements Serializable {
268 static final DoubleConverter INSTANCE = new DoubleConverter();
269
270 @Override
271 protected Double doForward(String value) {
272 return Double.valueOf(value);
273 }
274
275 @Override
276 protected String doBackward(Double value) {
277 return value.toString();
278 }
279
280 @Override
281 public String toString() {
282 return "Doubles.stringConverter()";
283 }
284
285 private Object readResolve() {
286 return INSTANCE;
287 }
288 private static final long serialVersionUID = 1;
289 }
290
291
292
293
294
295
296
297 @Beta
298 public static Converter<String, Double> stringConverter() {
299 return DoubleConverter.INSTANCE;
300 }
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318 public static double[] ensureCapacity(
319 double[] array, int minLength, int padding) {
320 checkArgument(minLength >= 0, "Invalid minLength: %s", minLength);
321 checkArgument(padding >= 0, "Invalid padding: %s", padding);
322 return (array.length < minLength)
323 ? copyOf(array, minLength + padding)
324 : array;
325 }
326
327
328 private static double[] copyOf(double[] original, int length) {
329 double[] copy = new double[length];
330 System.arraycopy(original, 0, copy, 0, Math.min(original.length, length));
331 return copy;
332 }
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348 public static String join(String separator, double... array) {
349 checkNotNull(separator);
350 if (array.length == 0) {
351 return "";
352 }
353
354
355 StringBuilder builder = new StringBuilder(array.length * 12);
356 builder.append(array[0]);
357 for (int i = 1; i < array.length; i++) {
358 builder.append(separator).append(array[i]);
359 }
360 return builder.toString();
361 }
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379 public static Comparator<double[]> lexicographicalComparator() {
380 return LexicographicalComparator.INSTANCE;
381 }
382
383 private enum LexicographicalComparator implements Comparator<double[]> {
384 INSTANCE;
385
386 @Override
387 public int compare(double[] left, double[] right) {
388 int minLength = Math.min(left.length, right.length);
389 for (int i = 0; i < minLength; i++) {
390 int result = Double.compare(left[i], right[i]);
391 if (result != 0) {
392 return result;
393 }
394 }
395 return left.length - right.length;
396 }
397 }
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414 public static double[] toArray(Collection<? extends Number> collection) {
415 if (collection instanceof DoubleArrayAsList) {
416 return ((DoubleArrayAsList) collection).toDoubleArray();
417 }
418
419 Object[] boxedArray = collection.toArray();
420 int len = boxedArray.length;
421 double[] array = new double[len];
422 for (int i = 0; i < len; i++) {
423
424 array[i] = ((Number) checkNotNull(boxedArray[i])).doubleValue();
425 }
426 return array;
427 }
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446 public static List<Double> asList(double... backingArray) {
447 if (backingArray.length == 0) {
448 return Collections.emptyList();
449 }
450 return new DoubleArrayAsList(backingArray);
451 }
452
453 @GwtCompatible
454 private static class DoubleArrayAsList extends AbstractList<Double>
455 implements RandomAccess, Serializable {
456 final double[] array;
457 final int start;
458 final int end;
459
460 DoubleArrayAsList(double[] array) {
461 this(array, 0, array.length);
462 }
463
464 DoubleArrayAsList(double[] array, int start, int end) {
465 this.array = array;
466 this.start = start;
467 this.end = end;
468 }
469
470 @Override public int size() {
471 return end - start;
472 }
473
474 @Override public boolean isEmpty() {
475 return false;
476 }
477
478 @Override public Double get(int index) {
479 checkElementIndex(index, size());
480 return array[start + index];
481 }
482
483 @Override public boolean contains(Object target) {
484
485 return (target instanceof Double)
486 && Doubles.indexOf(array, (Double) target, start, end) != -1;
487 }
488
489 @Override public int indexOf(Object target) {
490
491 if (target instanceof Double) {
492 int i = Doubles.indexOf(array, (Double) target, start, end);
493 if (i >= 0) {
494 return i - start;
495 }
496 }
497 return -1;
498 }
499
500 @Override public int lastIndexOf(Object target) {
501
502 if (target instanceof Double) {
503 int i = Doubles.lastIndexOf(array, (Double) target, start, end);
504 if (i >= 0) {
505 return i - start;
506 }
507 }
508 return -1;
509 }
510
511 @Override public Double set(int index, Double element) {
512 checkElementIndex(index, size());
513 double oldValue = array[start + index];
514
515 array[start + index] = checkNotNull(element);
516 return oldValue;
517 }
518
519 @Override public List<Double> subList(int fromIndex, int toIndex) {
520 int size = size();
521 checkPositionIndexes(fromIndex, toIndex, size);
522 if (fromIndex == toIndex) {
523 return Collections.emptyList();
524 }
525 return new DoubleArrayAsList(array, start + fromIndex, start + toIndex);
526 }
527
528 @Override public boolean equals(Object object) {
529 if (object == this) {
530 return true;
531 }
532 if (object instanceof DoubleArrayAsList) {
533 DoubleArrayAsList that = (DoubleArrayAsList) object;
534 int size = size();
535 if (that.size() != size) {
536 return false;
537 }
538 for (int i = 0; i < size; i++) {
539 if (array[start + i] != that.array[that.start + i]) {
540 return false;
541 }
542 }
543 return true;
544 }
545 return super.equals(object);
546 }
547
548 @Override public int hashCode() {
549 int result = 1;
550 for (int i = start; i < end; i++) {
551 result = 31 * result + Doubles.hashCode(array[i]);
552 }
553 return result;
554 }
555
556 @Override public String toString() {
557 StringBuilder builder = new StringBuilder(size() * 12);
558 builder.append('[').append(array[start]);
559 for (int i = start + 1; i < end; i++) {
560 builder.append(", ").append(array[i]);
561 }
562 return builder.append(']').toString();
563 }
564
565 double[] toDoubleArray() {
566
567 int size = size();
568 double[] result = new double[size];
569 System.arraycopy(array, start, result, 0, size);
570 return result;
571 }
572
573 private static final long serialVersionUID = 0;
574 }
575 }